// 这是模板默认生成的一个函数，用于记录点击按钮次数的
export function setupCounter(element: HTMLButtonElement) {
  let counter = 0
  const setCounter = (count: number) => {
    counter = count
    element.innerHTML = `count is ${counter}`
  }
  element.addEventListener('click', () => setCounter(++counter))
  setCounter(0)
}

/**
 * 格式化文件大小
 * 
 * @description 将文件大小进行合理格式化
 * 
 * @param size 文件大小
 * 
 * @return 格式化后的结果
 */
export const formtFileSize = (size?: string): string => {
  if (!size) {
    return '0B';
  }
  const unitArr: string[] = [
    'B',
    'KB',
    'MB',
    'GB',
    'TB',
    'PB',
    'EB',
    'ZB',
    'YB'
  ]
  let index: number = 0;
  const srcSize: number = parseFloat(size);
  index = Math.floor(Math.log(srcSize)/Math.log(1024))
  let sizes: number = srcSize / Math.pow(1024, index);
  sizes = Number(sizes.toFixed(2));
  return sizes + unitArr[index]; 
}